package com.jtech.together.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jtech.together.R;
import com.jtech.together.util.ImageUtils;
import com.jtechlib2.adapter.RecyclerAdapter;
import com.jtechlib2.view.RecyclerHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 动态的图片列表适配器
 * Created by wuxubaiyang on 2017/5/11.
 */

public class TrendsImageAdapter extends RecyclerAdapter<String> {
    private static final int VIEW_TYPE_ADD = 0x4512;
    private static final int ITEM_MAX_COUNT = 9;

    //图片的大小
    private int itemSize;
    private Map<String, String> imageMap;

    public TrendsImageAdapter(Context context, int itemSize) {
        super(context);
        this.itemSize = itemSize;
        this.imageMap = new HashMap<>();
    }

    @Override
    public int getItemCount() {
        int itemCount = getRealDatasSize();
        return itemCount < ITEM_MAX_COUNT ? itemCount + 1 : itemCount;
    }

    @Override
    public int getItemViewType(int position) {
        if (getRealDatasSize() < ITEM_MAX_COUNT && position == getItemCount() - 1) {
            return VIEW_TYPE_ADD;
        }
        return super.getItemViewType(position);
    }

    /**
     * 判断是否为添加按钮
     *
     * @param position
     * @return
     */
    public boolean isAddView(int position) {
        return VIEW_TYPE_ADD == getItemViewType(position);
    }

    /**
     * 获取剩余的数量
     *
     * @return
     */
    public int getLastCount() {
        return ITEM_MAX_COUNT - getRealDatasSize();
    }

    /**
     * 移除一个图片
     *
     * @param position
     */
    public void removeItem(int position) {
        //从map中移除
        imageMap.remove(getRealDatas().get(position));
        //移除item
        removeData(position);
    }

    /**
     * 获取图片集合
     *
     * @return
     */
    public List<String> getImageList() {
        List<String> imageList = new ArrayList<>();
        Iterator<String> iterator = imageMap.keySet().iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            imageList.add(imageMap.get(key));
        }
        return imageList;
    }

    /**
     * 添加多个图片
     *
     * @param images
     */
    public void addItem(List<String> images) {
        getRealDatas().addAll(images);
        notifyDataSetChanged();
        //遍历并添加到map
        for (int i = 0; i < images.size(); i++) {
            imageMap.put(images.get(i), images.get(i));
        }
    }

    /**
     * 替换item
     *
     * @param key
     * @param imageUrl
     */
    public void replaceItem(String key, String imageUrl) {
        imageMap.put(key, imageUrl);
    }

    @Override
    protected View createView(LayoutInflater layoutInflater, ViewGroup viewGroup, int viewType) {
        if (viewType == VIEW_TYPE_ADD) {
            return layoutInflater.inflate(R.layout.view_trends_image_add, viewGroup, false);
        }
        return layoutInflater.inflate(R.layout.view_trends_image, viewGroup, false);
    }

    @Override
    protected void convert(RecyclerHolder holder, String model, int position) {
        //设置item大小
        ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
        layoutParams.width = itemSize;
        layoutParams.height = itemSize;
        holder.itemView.setLayoutParams(layoutParams);
        //显示图片
        if (getItemViewType(position) != VIEW_TYPE_ADD) {
            ImageUtils.showImage(getContext(), model, holder.getImageView(R.id.imageview_trends_image));
        }
    }
}